package cn.sinata.wanglin.neighbor.fragment;

import android.text.TextUtils;

import com.easemob.redpacketui.RedPacketConstant;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseConversationListFragment;
import com.hyphenate.easeui.widget.EaseConversationList;
import com.hyphenate.exceptions.HyphenateException;
import com.xilada.xldutils.utils.Utils;

import cn.sinata.wanglin.R;

/**
 *
 * Created by liaoxiang on 17/1/12.
 */

public class ConversationListFragment extends EaseConversationListFragment implements EaseConversationList.EaseConversationListHelper{
    @Override
    protected void setUpView() {
        super.setUpView();
        conversationListView.init(conversationList,this);
    }

    @Override
    public String onSetItemSecondaryText(EMMessage message) {
        String digest = "";
        switch (message.getType()) {
            case LOCATION:
                if (message.direct() == EMMessage.Direct.RECEIVE) {
//                    digest = getString(R.string.location_recv);
//                    User user = UserDao.getInstance(getContext()).getUser(message.getFrom());
//                    if (user != null) {
//                        digest = String.format(digest,user.getName());
//                    } else {
//                        digest = String.format(digest,message.getFrom() );
//                    }
                    digest = "[位置]";
                    return digest;
                } else {
                    digest = getString(R.string.location_prefix);
                }
                break;
            case IMAGE:
                digest = getString(R.string.picture);
                break;
            case VOICE:
                digest = getString(R.string.voice_prefix);
                break;
            case VIDEO:
                digest = getString(R.string.video);
                break;
            case TXT:
                EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
                if(message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_VOICE_CALL, false)){
                    digest = getString(R.string.voice_call) + txtBody.getMessage();
                }else if(message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_VIDEO_CALL, false)){
                    digest = getString(R.string.video_call) + txtBody.getMessage();
                }else if(message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_BIG_EXPRESSION, false)){
                    if(!TextUtils.isEmpty(txtBody.getMessage())){
                        digest = txtBody.getMessage();
                    }else{
                        digest = getString(R.string.dynamic_expression);
                    }
                } else if (message.getBooleanAttribute(RedPacketConstant.MESSAGE_ATTR_IS_RED_PACKET_ACK_MESSAGE, false)) {
                    try {
                        String currentUser = EMClient.getInstance().getCurrentUser();
                        String fromUser = message.getStringAttribute(RedPacketConstant.EXTRA_RED_PACKET_SENDER_NAME);//红包发送者
                        String toUser = message.getStringAttribute(RedPacketConstant.EXTRA_RED_PACKET_RECEIVER_NAME);//红包接收者
                        String senderId;
                        if (message.direct() == EMMessage.Direct.SEND) {
                            if (message.getChatType().equals(EMMessage.ChatType.GroupChat)) {
                                senderId = message.getStringAttribute(RedPacketConstant.EXTRA_RED_PACKET_SENDER_ID);
                                if (senderId.equals(currentUser)) {
                                    digest=getString(R.string.msg_take_red_packet);
                                } else {
                                    digest= String.format(getResources().getString(R.string.msg_take_someone_red_packet), fromUser);
                                }
                            } else {
                                digest=String.format(getResources().getString(R.string.msg_take_someone_red_packet), fromUser);
                            }
                        } else {
                            digest=String.format(getResources().getString(R.string.msg_someone_take_red_packet), toUser);
                        }
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                    }
                } else {
                    digest = txtBody.getMessage();
                }
                break;
            case FILE:
                digest = getString(R.string.file);
                break;
            default:
                Utils.systemErr("error, unknow type");
                return "";
        }

        return digest;
    }
}
